package bamin.com.kepiao.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.aiton.administrator.shane_library.shane.utils.GsonUtils;
import com.aiton.administrator.shane_library.shane.utils.HTTPUtils;
import com.aiton.administrator.shane_library.shane.utils.VolleyListener;
import com.android.volley.VolleyError;

import java.util.HashMap;
import java.util.Map;

import bamin.com.kepiao.R;
import bamin.com.kepiao.constant.Constant;
import bamin.com.kepiao.models.VersionAndHouTaiIsCanUse;


public class WelcomeActivity extends Activity {
    Handler hand = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        checkVersionAndHouTaiIsCanUse();
    }

    private void startActivity() {
        SharedPreferences sp = getSharedPreferences(Constant.SP_KEY.SP_ISFRIST, 0);
        final boolean isfrist = sp.getBoolean("isfrist", true);
        hand.postDelayed(new Runnable() {

            @Override
            public void run() {
                Intent intent = new Intent();
                if (isfrist) {

                    intent.setClass(WelcomeActivity.this, GuideActivity.class);
                } else {
                    intent.setClass(WelcomeActivity.this, MainActivity.class);
                }
                startActivity(intent);
                finish();
            }
        }, 1000);
    }

    private void checkVersionAndHouTaiIsCanUse() {
        String url = Constant.Url.CHECKLIVE_TICKET;
        Map<String, String> map = new HashMap<>();
        map.put("flag", Constant.FLAG);
        HTTPUtils.post(WelcomeActivity.this, url, map, new VolleyListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("onErrorResponse ", "checklive请求失败");
                setDialogCkeck("服务器正在升级，暂停服务", "确认");
            }

            @Override
            public void onResponse(String s) {
                Log.e("onResponse ", "checklive--" + s);
                VersionAndHouTaiIsCanUse versionAndHouTaiIsCanUse = GsonUtils.parseJSON(s, VersionAndHouTaiIsCanUse.class);
                int ableVersion = versionAndHouTaiIsCanUse.getAbleVersion();
                Log.e("onResponse ", "服务器是否存活" + versionAndHouTaiIsCanUse.isLive());
                if (versionAndHouTaiIsCanUse.isLive()) {
                    startActivity();
                } else {
                    setDialogCkeck(versionAndHouTaiIsCanUse.getMessage(), "确认");
                }
            }
        });
    }

    //dialog提示
    private void setDialogCkeck(String messageTxt, String iSeeTxt) {
        View commit_dialog = getLayoutInflater().inflate(R.layout.commit_dialog, null);
        TextView message = (TextView) commit_dialog.findViewById(R.id.message);
        Button ISee = (Button) commit_dialog.findViewById(R.id.ISee);
        message.setText(messageTxt);
        ISee.setText(iSeeTxt);
        AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this);
        final AlertDialog dialog = builder.setView(commit_dialog)
                .create();
        dialog.setCancelable(false);
        dialog.show();
        commit_dialog.findViewById(R.id.ISee).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });
    }
}